package com.whsxt.buss.controller;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whsxt.buss.data.SalesData;
import com.whsxt.buss.domain.BusCustomer;
import com.whsxt.buss.domain.BusGoods;
import com.whsxt.buss.domain.BusSales;
import com.whsxt.buss.service.IBusCustomerService;
import com.whsxt.buss.service.IBusGoodsService;
import com.whsxt.buss.service.IBusSalesService;
import com.whsxt.common.CodeMsg;
import com.whsxt.common.PageInfo;
import com.whsxt.common.Result;
import cn.hutool.core.util.StrUtil;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 贺炯
 * @since 2019-09-28
 */
@RestController
@RequestMapping("/buss/sales")
public class BusSalesController {
	
	@Autowired
	private IBusSalesService iBusSalesService;
	
	@Autowired
	private IBusGoodsService iBusGoodsService;
	
	@Autowired
	private IBusCustomerService IBusCustomerService;
	
	@RequestMapping("/list")
	public Object list(SalesData data) {
		IPage<BusSales> page = new Page<BusSales>(data.getPage(), data.getLimit());
		QueryWrapper<BusSales> queryWrapper = new QueryWrapper<BusSales>();
		queryWrapper.eq(data.getCustomerid()!=null&&data.getCustomerid()!= ' ', "customerid", data.getCustomerid());
		queryWrapper.eq(data.getGoodsid()!=null&&data.getGoodsid()!= ' ', "goodsid", data.getGoodsid());
		queryWrapper.gt(StrUtil.isNotBlank(data.getStartTime()), "salestime", data.getStartTime());
		queryWrapper.lt(StrUtil.isNotBlank(data.getEndTime()), "salestime", data.getEndTime());
		iBusSalesService.page(page,queryWrapper);
		List<BusSales> records = page.getRecords();
		for (BusSales busSales : records) {
			Integer goodsid = busSales.getGoodsid();
			BusGoods byId = iBusGoodsService.getById(goodsid);
			busSales.setGoodsname(byId.getGoodsname());
			busSales.setSize(byId.getSize());
			Integer customerid = busSales.getCustomerid();
			BusCustomer byId2 = IBusCustomerService.getById(customerid);
			busSales.setCustomername(byId2.getCustomername());
		}
		PageInfo pageInfo = new PageInfo(page.getTotal(), records);
		return new Result(pageInfo);
	}
	/**
	 *  获取客户的下拉框数据
	 * @return
	 */
	@RequestMapping("/getCustomer")
	public Object getCustomer() {
		List<BusCustomer> list = IBusCustomerService.list();
		return new Result(list);
	}
	
	
	/**
	 *  	新增 销售
	 * @param inport
	 * @return
	 */
	@RequestMapping("/addSales")
	public Object addSales(BusSales sales) {
		try {
			Result rs = (Result) iBusSalesService.addSales(sales);
			return rs;
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(CodeMsg.SALES_ADD_ERROR);
		}
	}
	/**
	 *	  更新销售数据
	 * @param sales
	 * @return
	 */
	@RequestMapping("/updateSales")
	public Object updateSales(BusSales sales) {
		try {
			Result rs = iBusSalesService.updateSales(sales);
			return rs;
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(CodeMsg.SALES_UPDATE_ERROE);
		}
	}
	
	@RequestMapping("/deleteOne")
	public Object deleteOne(Integer id) {
		try {
			Result rs = iBusSalesService.deleteOne(id);
			return rs;
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(CodeMsg.SALES_DELETE_ERROE);
		}
	}
	/**
	 *  销售退货
	 * @param id
	 * @param number
	 * @param remark
	 * @return
	 */
	@RequestMapping("/back")
	public Object back(Integer id,Integer number,String remark) {
		try {
			Result rs = iBusSalesService.back(id,number,remark);
			return rs;
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(CodeMsg.SALES_BACK_ERROE);
		}
	}
}

